//
//  BLTimeAxisRenderer.m
//  BLTimeAxis
//
//  Created by dg on 2025/4/18.
//

#import "BLTimeAxisRenderer.h"

@implementation BLTimeAxisRenderer

- (instancetype)initWithViewSize:(CGSize)viewSize {
    if (self = [super init]) {
        [self setViewSize:viewSize];
    }
    return self;
}

- (instancetype)initWithViewSize:(CGSize)viewSize context:(CGContextRef)context {
    if (self = [self initWithViewSize:viewSize]) {
        [self setContext:context];
        
        self.viewWidth = viewSize.width;
        self.viewHeight = viewSize.height;
    }
    
    return self;
}

#pragma mark - update values method
- (void)updateValueWithTimeAxisDigitalDivision:(BLTimeAxisDigitalDivision *)aTimeAxisDigitalDivision {
    CGFloat width = _axisDirection == BLTimeAxisDirectionHorizontal ? self.viewWidth : self.viewHeight;
    _aSecondOfPixel = [aTimeAxisDigitalDivision aSecondOfPixelWithViewWidth:width];
    
    // 可见最小时间 = 当前时间(s) - 刻度线偏移位置(像素)/每秒对应的宽度(像素/s)
    _minTimeInVisible = _currentTimeInterval - _ruleFixedOffset/_aSecondOfPixel;
    // 可见最大时间 = 当前时间(s) + （可见视图宽度(像素) - 刻度线偏移位置(像素))/每秒对应的宽度(像素/s)
    _maxTimeInVisible = _currentTimeInterval + (width - _ruleFixedOffset)/_aSecondOfPixel;
}

- (void)updateValueWithTimeAxisRule:(BLTimeAxisRule *)aTimeAxisRule {
    _currentTimeInterval = aTimeAxisRule.currentTimeInterval;
    _ruleFixedOffset = aTimeAxisRule.fixedOffset;
    _currentHourDate = aTimeAxisRule.currentHourDate;
    _currentHour = aTimeAxisRule.currentHour;
    _axisDirection = aTimeAxisRule.axisDirection;
}

- (void)updateValueWithTimeAxisBaseLine:(BLTimeAxisBaseLine *)aTimeAxisBaseLine {
    CGFloat width = _axisDirection == BLTimeAxisDirectionHorizontal ? self.viewWidth : self.viewHeight;
    _aSecondOfPixel = [aTimeAxisBaseLine aSecondOfPixelWithViewWidth:width];
    _baseLineFixedOffset = aTimeAxisBaseLine.fixedOffset;
}

- (void)updateValueWithTimeAxisBackground:(BLTimeAxisBackground *)aTimeAxisBackground {
    _backgroundSize = aTimeAxisBackground.strokeSize;
}

#pragma mark -- public method

- (void)visitTimeAxis:(id<BLTimeAxisComponent>)aTimeAxis {
    // 默认行为
    CGContextSetStrokeColorWithColor(_context, [aTimeAxis.strokeColor CGColor]);
    CGContextSetLineWidth(_context, aTimeAxis.strokeSize);
}

//绘制数据
- (void)visitTimeAxisData:(BLTimeAxisData *)aTimeAxisData {
    // 设置色彩，宽度
    [self visitTimeAxis:(id<BLTimeAxisComponent>)aTimeAxisData];
}

// 绘制时间轴每段
- (void)visitTimeAxisDigitalDivision:(BLTimeAxisDigitalDivision *)aTimeAxisDigitalDivision {
    [self updateValueWithTimeAxisDigitalDivision:aTimeAxisDigitalDivision];
    
    [self visitTimeAxis:(id<BLTimeAxisComponent>)aTimeAxisDigitalDivision];
}

- (void)visitTimeAxisRule:(BLTimeAxisRule *)aTimeAxisRule {
    [self updateValueWithTimeAxisRule:aTimeAxisRule];
    
    [self visitTimeAxis:(id<BLTimeAxisComponent>)aTimeAxisRule];
}


- (void)visitTimeAxisBaseLine:(BLTimeAxisBaseLine *)aTimeAxisBaseLine {
    // 更新值
    [self updateValueWithTimeAxisBaseLine:aTimeAxisBaseLine];
    
    // 设置色彩，宽度
    [self visitTimeAxis:(id<BLTimeAxisComponent>)aTimeAxisBaseLine];
}

- (void)visitTimeAxisBackground:(BLTimeAxisBackground *)background {
    // 更新值
    [self updateValueWithTimeAxisBackground:background];
    
    // 设置色彩,宽度
    [self visitTimeAxis:(id<BLTimeAxisComponent>)background];
}

- (void)drawLineWithContext:(CGContextRef)context from:(CGPoint)from to:(CGPoint)to {
    CGContextMoveToPoint(context, from.x, from.y);
    CGContextAddLineToPoint(context, to.x, to.y);
    CGContextStrokePath(context);
}

@end
